import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/AccessDenied',
    name: 'AccessDeniedView',
    component: () => import('../views/notice/AccessDenied.vue')
  },
  {
    path: '/Login',
    name: 'LoginView',
    component: () => import('../views/login/LoginView.vue')
  },
  {
    path: '/',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
    redirect: '/Login',
  },
  {
    path: '/Home',
    name: 'home',
    component: HomeView,
    redirect: to => {
      if (localStorage.getItem("currentRole") === "综合办") {
        to = '/Home/HomeAdmin';
        return to
      }
      else {
        to = '/Home/HomeOthers';
        return to
      }
    },
    children: [
      //角色管理
      {
        path: 'RoleManagement',
        name: 'RoleView',
        component: () => import('../views/systemManagement/RoleView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      //菜单管理
      {
        path: 'MenuManagement',
        name: 'MenuView',
        component: () => import('../views/systemManagement/MenuView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      {
        path: 'HomeAdmin',
        name: 'DataView',
        component: () => import('../views/home/DataView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/Home/HomeOthers',
              });
            }
          } else {
            next();
          }
        },
      },
      // 在框架内打开告警页面
      {
        path: 'Warn',
        name: 'WarnView',
        component: () => import('../views/home/WarnView.vue')
      },
      // 在框架内打开预警页面
      {
        path: 'Alarm',
        name: 'AlarmView',
        component: () => import('../views/home/AlarmView.vue')
      },
      // 在框架内打开待办页面
      {
        path: 'ToDoContainer',
        name: 'ToDoContainer',
        component: () => import('../views/home/ToDoContainer.vue'),
        redirect: '/Home/ToDoContainer/ToDoUndone',
        children: [
          {
            path: 'ToDoDone',
            name: 'ToDoDoneView',
            component: () => import('../views/home/ToDoDoneView.vue')
          },
          {
            path: 'ToDoUndone',
            name: 'ToDoUnDoneView',
            component: () => import('../views/home/ToDoUnDoneView.vue')
          },
        ],
      },
      {
        path: 'HomeOthers',
        name: 'Data1View',
        meta: { requireAuth: true },
        component: () => import('../views/home/DataView1.vue'),
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole !== '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/Home/HomeAdmin',
              });
            }
          } else {
            next();
          }
        },
      },
      //用户管理
      {
        path: 'UserManagement',
        name: 'UserView',
        component: () => import('../views/systemManagement/UserView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      // 在框架中打开用户详情
      {
        path: 'UserDetail',
        name: 'UserDetailView',
        component: () => import('../views/systemManagement/UserDetailView.vue')
      },

      // 用车申请（用车人）
      {
        path: 'AutoOrder',
        name: 'UseCarView',
        component: () => import('../views/orderCenter/UseCarView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '用车人') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/Home/AutoOrderOthers',
              });
            }
          } else {
            next();
          }
        },
      },
      //用车申请（其他）
      {
        path: 'AutoOrderOthers',
        name: 'AutoOrderOthersView',
        component: () => import('../views/orderCenter/AutoOrderOthers.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '用车人') { // 查询本地存储信息是否已经登陆
              next({
                path: '/Home/AutoOrderOthers',
              });
            } else {
              next();
            }
          } else {
            next();
          }
        },
      },
      //用车申请详情
      {
        path: 'AutoOrderDetail',
        name: 'AutoOrderDetailView',
        component: () => import('../views/orderCenter/AutoOrderDetail.vue')
      },

      //维修申请（驾驶员）
      {
        path: 'RepairOrder',
        name: 'FixView',
        component: () => import('../views/orderCenter/FixView.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '驾驶员') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/Home/RepairOrderOthers',
              });
            }
          } else {
            next();
          }
        },
      },

      //维修申请（其他）
      {
        path: 'RepairOrderOthers',
        name: 'RepairOrderOthersView',
        component: () => import('../views/orderCenter/RepairOrderOthers.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '驾驶员') { // 查询本地存储信息是否已经登陆
              next({
                path: '/Home/RepairOrder',
              });
            } else {
              next();
            }
          } else {
            next();
          }
        },
      },

      //维修申请详情
      {
        path: 'RepairOrderDetail',
        name: 'RepairOrderDetailView',
        component: () => import('../views/orderCenter/RepairOrderDetail.vue')
      },
      {
        path: 'ProcessLog',
        name: 'ProcessLog',
        component: () => import('../views/regulatoryAnalysis/processLog.vue')
      },
      {
        path: 'LocationManagement',
        name: 'locationmanage',
        component: () => import('../views/regulatoryAnalysis/locationManage.vue')
      },
      {
        path: 'AutoArchives',
        name: 'autoarchives',
        component: () => import('../views/archives/AutoArchives.vue')
      },
      {
        path: 'DriverArchives',
        name: 'driverarchives',
        component: () => import('../views/archives/DriverArchives.vue')
      },
      {
        path: 'OrderArchives',
        name: 'orderarchives',
        component: () => import('../views/archives/OrderArchives.vue'),
        redirect: '/Home/OrderArchives/AutoOrderArchives',
        children: [
          {
            path: 'AutoOrderArchives',
            name: 'AutoOrderArchivesView',
            component: () => import('../views/archives/AutoOrderArchives.vue'),
            meta: { requireAuth: true },
            beforeEnter: (to, from, next) => {
              if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
                var currentRole = localStorage.getItem('currentRole');
                if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
                  next();
                } else {
                  next({
                    path: '/AccessDenied',
                  });
                }
              } else {
                next();
              }
            },
          },
          {
            path: 'RepairOrderArchives',
            name: 'RepairOrderArchivesView',
            component: () => import('../views/archives/RepairOrderArchives.vue'),
            meta: { requireAuth: true },
            beforeEnter: (to, from, next) => {
              if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
                var currentRole = localStorage.getItem('currentRole');
                if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
                  next();
                } else {
                  next({
                    path: '/AccessDenied',
                  });
                }
              } else {
                next();
              }
            },
          },
        ]
      },
      {
        path: 'QueryBills',
        name: 'querybills',
        component: () => import('../views/billManage/QueryBills.vue')
      },
      {
        path: 'BillDetail',
        name: 'billdetail',
        component: () => import('../views/billManage/BillDetail.vue')
      },
      {
        path: 'AutoApplication',
        name: 'AutoApplicationView',
        component: () => import('../views/application/AutoApplication.vue')
      },
      {
        path: 'RepairApplication',
        name: 'RepairApplicationView',
        component: () => import('../views/application/RepairApplication.vue')
      },
      {
        path: 'AddAutoArchives',
        name: 'AddAutoArchivesView',
        component: () => import('../views/add/AddAutoArchives.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      {
        path: 'AddDriverArchives',
        name: 'AddDriverArchivesView',
        component: () => import('../views/add/AddDriverArchives.vue'), meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      //添加用户
      {
        path: 'AddUser',
        name: 'AddUserView',
        component: () => import('../views/add/AddUser.vue'),
        meta: { requireAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
            var currentRole = localStorage.getItem('currentRole');
            if (currentRole === '综合办') { // 查询本地存储信息是否已经登陆
              next();
            } else {
              next({
                path: '/AccessDenied',
              });
            }
          } else {
            next();
          }
        },
      },
      {
        path: 'BillApplication',
        name: 'BillApplicationView',
        component: () => import('../views/application/BillApplication.vue')
      },
      {
        path: 'AutoDepartmentApproval',
        name: 'AutoDepartmentApprovalView',
        component: () => import('../views/approval/AutoDepartmentApproval.vue')
      },
      {
        path: 'AutoGeneralApproval',
        name: 'AutoGeneralApprovalView',
        component: () => import('../views/approval/AutoGeneralApproval.vue')
      },
      {
        path: 'RepairGeneralApproval',
        name: 'RepairGeneralApprovalView',
        component: () => import('../views/approval/RepairGeneralApproval.vue')
      },
      //申请通过返给申请人的单子
      {
        path: 'AutoApplicationApproved',
        name: 'AutoApplicationApprovedView',
        component: () => import("../views/notice/AutoApplicationApproved.vue")
      },
      {
        path: 'RepairApplicationApproved',
        name: 'RepairApplicationApprovedView',
        component: () => import("../views/notice/RepairApplicationApproved.vue")
      },
      //用车委派单
      {
        path: 'AutoDelegation',
        name: 'AutoDelegationView',
        component: () => import("../views/notice/AutoDelegation.vue")
      },
      //完成的待办
      {
        path: 'AutoDelegationDone',
        name: 'AutoDelegationDoneView',
        component: () => import("../views/ToDoDoneDetail/AutoDelegationDone.vue")
      },
      {
        path: 'AutoDepartmentApprovalDone',
        name: 'AutoDepartmentApprovalDoneView',
        component: () => import("../views/ToDoDoneDetail/AutoDepartmentApprovalDone.vue")
      },
      {
        path: 'AutoGeneralApprovalDone',
        name: 'AutoGeneralApprovalDoneView',
        component: () => import("../views/ToDoDoneDetail/AutoGeneralApprovalDone.vue")
      },
      {
        path: 'AutoNoticeDone',
        name: 'AutoNoticeDoneView',
        component: () => import("../views/ToDoDoneDetail/AutoNoticeDone.vue")
      },
      {
        path: 'RepairGeneralApprovalDone',
        name: 'RepairGeneralApprovalDoneView',
        component: () => import("../views/ToDoDoneDetail/RepairGeneralApprovalDone.vue")
      },
      {
        path: 'RepairNoticeDone',
        name: 'RepairNoticeDoneView',
        component: () => import("../views/ToDoDoneDetail/RepairNoticeDone.vue")
      },
      //车辆档案修改
      {
        path: '/UpgradeAutoArchives',
        name: 'upgradeautoarchives',
        component: () => import('../views/upgrade/UpgradeAutoArchives.vue')
      },
      //驾驶员档案修改
      {
        path: '/UpgradeDriverArchives',
        name: 'upgradedriverarchives',
        component: () => import('../views/upgrade/UpgradeDriverArchives.vue')
      },
    ]
  },
]


let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function (location, res, rej) {
  if (res && rej) {
    originPush.call(this, location, res, rej)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}

VueRouter.prototype.replace = function (location, res, rej) {
  if (res && rej) {
    originReplace.call(this, location, res, rej)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}

const router = new VueRouter({
  routes
})

export default router
